import 'package:cbec_app/utils/Tools.dart';
import 'package:get/get.dart';
import '../../../utils/AppData.dart';
import '../../http/network.dart';
import 'goods_group_header_tab_item_entity.dart';

class DecorationGoodsGroupCtr extends GetxController {
  var curIndex = 0.obs;
  int pageNum = 1;
  int pageSize = 10;
  RxList<GoodsGroupHeaderTabItemEntity> tabTitleModels = <GoodsGroupHeaderTabItemEntity>[].obs;
  late GoodsGroupHeaderTabItemEntity currentTabItem;
  RxList<dynamic> goodsList = <dynamic>[].obs;

  void fetchTabData(Map<String, dynamic> info) {
    var tempTabTitleModels = convertToModelList(info);
    if (isShowAllTab(info)) {
      tempTabTitleModels.insert(0, tempTabItem());
    }
    currentTabItem = tempTabTitleModels.first;
    print('tempTabTitleModels ======= $tempTabTitleModels');
    tabTitleModels.assignAll(tempTabTitleModels); // 自动刷新 UI
    // tabTitleModels.refresh();
    getGroupInfo();
  }

  List<GoodsGroupHeaderTabItemEntity> convertToModelList(Map<String, dynamic> info) {
    print('sub_entry ========= ${info['sub_entry']}');
    if (info['sub_entry'] is List) {
      return (info['sub_entry'] as List).map((item) => GoodsGroupHeaderTabItemEntity.fromJson(item)).toList();
    } else {
      return [];
    }
  }

  bool isShowAllTab(Map<String, dynamic> info) {
    return info['show_all_goods_tag'] == '1';
  }

  GoodsGroupHeaderTabItemEntity tempTabItem() {
    GoodsGroupHeaderTabItemEntity tabItem = GoodsGroupHeaderTabItemEntity();
    tabItem.channelNum = 10;
    tabItem.createTime = '';
    tabItem.goodsNumber = 10;
    tabItem.id = '';
    tabItem.isShowAll = true;
    tabItem.name = '全部';
    tabItem.tagName = '全部';
    return tabItem;
  }

  /// 获取商品分组信息
  void getGroupInfo() async {
    String groupId = currentTabItem.id!;
    bool isShowAll = currentTabItem.isShowAll ?? false; //是否显示全部
    pageSize = currentTabItem.goodsNumber ?? 10;
    onSelectGoodsGroup(groupId, curIndex.value, isShowAll ? 100 : pageSize, isLoading: false);
  }

  /// 获取商品列表
  Future<void> getGoodsList(String groupId, int num, int size) async {
    Map<String, dynamic> params = {};
    if (currentTabItem.id != null) {
      params['id'] = currentTabItem.id;
    }
    params['sid'] = AppData.sid;
    params['pageNum'] = num;
    params['pageSize'] = size;
    var data = await Network.getGoodsById(params);

    goodsList.assignAll(data['list']);
  }

  /// 监听选中的商品分组索引
  void onSelectGoodsGroup(String id, int index, int size, {bool isLoading = true}) async {
    curIndex.value = index;
    currentTabItem = tabTitleModels[index];
    var groupId = currentTabItem.id;
    if (isLoading) {
      Tools.showLoading();
    }
    await getGoodsList(groupId!, pageNum, size);
    if (isLoading) {
      Tools.hideLoading();
    }
    curIndex.refresh();
  }
}

/*
  {page_margin: 8,
  size_type: 0,
  show_origin_price: 0,
  display_scale: 1,
  image_fill_style: 1,
  type: goods_group,
  title: 1,
  corner_mark_type: 0,
  border_radius_type: 1,
  text_align_type: left,
  goods_margin: 8,
  show_corner_mark: 0,
  uuid: eeba5904-b3da-4361-a66f-1ef0f1c99b56,
  show_sold_num: 0,
  size: 2,
  nav_style: 0,
  text_style_type: 1,
  price: 1,
  sticky: 0,
  show_sub_title: 0,
  sub_entry: [
  {isShowAll: false,
  goods_number: 10,
  createTime: 2024-11-10 19:43:18,
  tag_name: 测试测试,
  name: 测试测试,
  channelNum: 2,
  id: 1855576966270857218}
  , {
  isShowAll: false,
  goods_number: 10,
  createTime: 2024-10-06 14:36:50,
  tag_name: 测试分组1,
  name: 测试分组1,
  channelNum: 7,
  id: 1842816262485954561
  }
  ],
  corner_mark_image: ,
  show_all_goods_tag: 0}
   */
